package com.imay.live.view.dialog;

import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.imay.base.fragment.BaseDialogFragment;
import com.imay.live.R;
import com.imay.live.activity.PermissionsGrantActivity;
import com.imay.live.activity.dynamics.RepostActivity;
import com.imay.live.helper.ShareSDKHelper;
import com.imay.live.model.FeedModel;
import com.imay.live.proto.helper.DefaultDeniedPermissionHandler;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.io.File;

/**
 * Created by Administrator on 2017/4/12.
 */

public class ShareDialogFragment extends BaseDialogFragment implements View.OnClickListener {
    private final static String BUNDLE_NAME_TITLE = "title";
    private final static String BUNDLE_NAME_DES = "des";
    private final static String BUNDLE_NAME_IMAGE = "image";
    private final static String BUNDLE_NAME_URL = "url";
    private final static String BUNDLE_NAME_FEED = "feed";
    private final static String BUNDLE_NAME_TYPE = "type";

    public final static int WEB = 1;
    public final static int IMG_FILE = 2;

    private int type;
    private String mTitle;
    private String mDes;
    private String mImage;
    private String mUrl;
    private FeedModel mFeed;    //是否含有转发

    public static ShareDialogFragment newInstance(int type, String url, String title, String des, String image) {
        return newInstance(type, url, title, des, image, null);
    }

    public static ShareDialogFragment newInstance(int type, String url, String title, String des, String image, FeedModel feed) {
        Bundle bundle = new Bundle();
        bundle.putString(BUNDLE_NAME_URL, url);
        bundle.putString(BUNDLE_NAME_TITLE, title);
        bundle.putString(BUNDLE_NAME_DES, des);
        bundle.putString(BUNDLE_NAME_IMAGE, image);
        bundle.putParcelable(BUNDLE_NAME_FEED, feed);
        bundle.putInt(BUNDLE_NAME_TYPE, type);
        ShareDialogFragment fragment = new ShareDialogFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_share;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        if (bundle != null) {
            mTitle = bundle.getString(BUNDLE_NAME_TITLE);
            mDes = bundle.getString(BUNDLE_NAME_DES);
            mImage = bundle.getString(BUNDLE_NAME_IMAGE);
            mUrl = bundle.getString(BUNDLE_NAME_URL);
            mFeed = bundle.getParcelable(BUNDLE_NAME_FEED);
            type = bundle.getInt(BUNDLE_NAME_TYPE);
        }

        if (mFeed != null) {
            $(R.id.bt_share_01).setVisibility(View.VISIBLE);
            $(R.id.bt_share_03_1).setVisibility(View.GONE);
            $(R.id.bt_share_03_2).setVisibility(View.VISIBLE);
        } else {
            $(R.id.bt_share_01).setVisibility(View.GONE);
            $(R.id.bt_share_03_1).setVisibility(View.VISIBLE);
            $(R.id.bt_share_03_2).setVisibility(View.GONE);
        }

        $(R.id.bt_share_01).setOnClickListener(this);
        $(R.id.bt_share_02).setOnClickListener(this);
        $(R.id.bt_share_03_1).setOnClickListener(this);
        $(R.id.bt_share_03_2).setOnClickListener(this);
        $(R.id.bt_share_04).setOnClickListener(this);
        $(R.id.bt_share_05).setOnClickListener(this);
        $(R.id.bt_share_06).setOnClickListener(this);
        //$(R.id.tv_dialog_share).setOnClickListener(this);
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    @Override
    protected boolean isMatchParent() {
        return false;
    }

    @Override
    protected int getHeight() {
        return WindowManager.LayoutParams.WRAP_CONTENT;
    }

    @Override
    protected boolean isOutsideTouchable() {
        return true;
    }

    @Override
    protected int getWindowAnimations() {
        return R.style.dialog_from_bottom;
    }

    @Override
    public void onClick(View view) {
        synchronized (view) {
            SHARE_MEDIA share_media = SHARE_MEDIA.MORE;
            switch (view.getId()) {
                case R.id.bt_share_01:
                    report();
                    break;
                case R.id.bt_share_02:
                    share_media = SHARE_MEDIA.WEIXIN;
                    break;
                case R.id.bt_share_03_1:
                case R.id.bt_share_03_2:
                    share_media = SHARE_MEDIA.WEIXIN_CIRCLE;
                    break;
                case R.id.bt_share_04:
                    share_media = SHARE_MEDIA.QQ;
                    break;
                case R.id.bt_share_05:
                    share_media = SHARE_MEDIA.QZONE;
                    break;
                case R.id.bt_share_06:
                    share_media = SHARE_MEDIA.SINA;
                    break;
            }
            if (share_media != SHARE_MEDIA.MORE) {
                if (type == WEB) {
                    share(getActivity(), share_media);
                } else if (type == IMG_FILE) {
                    shareImgFile(getActivity(), share_media);
                }
            }
            dismiss();
        }
    }

    private void share(final Activity activity, final SHARE_MEDIA shareMedia) {
        ShareSDKHelper.getBuilder(activity, shareMedia).share(mUrl, mTitle, mDes, mImage);
    }

    private void report() {
        RepostActivity.startActivity(getActivity(), mFeed);
    }

    private void shareImgFile(final Activity activity, final SHARE_MEDIA share_media) {
        File file = new File(mImage);
        if (file.exists()) {
            ShareSDKHelper.getBuilder(activity, ShareSDKHelper.TYPE.IMG, share_media).setImgFile(file).share();
        }
    }
}
